<template>
  <div class="error-collection">
    <!-- 面包屑导航 -->
    <div class="breadcrumb">
      <span class="breadcrumb-item">智能批改</span>
      <span class="breadcrumb-separator">/</span>
      <span class="breadcrumb-item current">错题集</span>
    </div>

    <!-- 主内容区域 -->
    <div class="main-content">
      <!-- 页面标题 -->
      <div class="page-header">
        <h1 class="page-title">错题集</h1>
      </div>

      <!-- 学科分类标签 -->
      <div class="subject-tabs">
        <div 
          v-for="subject in subjects" 
          :key="subject.key"
          class="subject-tab"
          :class="{ active: activeSubject === subject.key }"
          @click="setActiveSubject(subject.key)"
        >
          {{ subject.label }}
        </div>
      </div>

      <!-- 类型分类标签 -->
      <div class="type-tabs">
        <div 
          v-for="type in types" 
          :key="type.key"
          class="type-tab"
          :class="{ active: activeType === type.key }"
          @click="setActiveType(type.key)"
        >
          {{ type.label }}
        </div>
        
        <!-- 搜索框 -->
        <div class="search-box">
          <input 
            type="text" 
            placeholder="搜索" 
            v-model="searchKeyword"
            class="search-input"
          />
          <el-icon class="search-icon">
            <Search />
          </el-icon>
        </div>
      </div>

      <!-- 记录列表 -->
      <div class="records-grid">
        <RecordCard
          v-for="record in filteredRecords"
          :key="record.id"
          :record="record"
          :routeName="'teacher-error-collection'"
          @detail="handleRecordDetail"
          @delete="handleRecordDelete"
        />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter } from 'vue-router'
import { Search } from '@element-plus/icons-vue'
import RecordCard from '@/components/shared/Common/RecordCard.vue'

const router = useRouter()

// 学科分类数据
const subjects = [
  { key: 'all', label: '全部' },
  { key: 'math', label: '数学' },
  { key: 'english', label: '英语' },
  { key: 'chinese', label: '语文' },
  { key: 'other', label: '其他' }
]

// 类型分类数据
const types = [
  { key: 'all', label: '全部' },
  { key: 'homework-check', label: '作业检查' },
  { key: 'teaching-task', label: '教学任务' }
]

// 当前选中的分类
const activeSubject = ref('all')
const activeType = ref('all')
const searchKeyword = ref('')

// 模拟错题集记录数据
const records = ref([
  {
    id: 1,
    title: '数学错题-分数运算',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'math',
    type: 'homework-check'
  },
  {
    id: 2,
    title: '英语错题-语法练习',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'english',
    type: 'homework-check'
  },
  {
    id: 3,
    title: '语文错题-阅读理解',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'chinese',
    type: 'teaching-task'
  },
  {
    id: 4,
    title: '数学错题-几何证明',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'math',
    type: 'teaching-task'
  },
  {
    id: 5,
    title: '物理错题-力学分析',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'other',
    type: 'homework-check'
  },
  {
    id: 6,
    title: '英语错题-单词拼写',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'english',
    type: 'homework-check'
  },
  {
    id: 7,
    title: '语文错题-古诗词默写',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'chinese',
    type: 'teaching-task'
  },
  {
    id: 8,
    title: '数学错题-函数图像',
    time: new Date('2021-10-12 00:00:00'),
    subject: 'math',
    type: 'teaching-task'
  }
])

// 根据分类和搜索关键词筛选记录
const filteredRecords = computed(() => {
  let filtered = records.value

  // 按学科筛选
  if (activeSubject.value !== 'all') {
    filtered = filtered.filter(record => record.subject === activeSubject.value)
  }

  // 按类型筛选
  if (activeType.value !== 'all') {
    filtered = filtered.filter(record => record.type === activeType.value)
  }

  // 按搜索关键词筛选
  if (searchKeyword.value.trim()) {
    filtered = filtered.filter(record => 
      record.title.toLowerCase().includes(searchKeyword.value.toLowerCase())
    )
  }

  return filtered
})

// 设置活跃学科
const setActiveSubject = (subject: string) => {
  activeSubject.value = subject
}

// 设置活跃类型
const setActiveType = (type: string) => {
  activeType.value = type
}

// 记录操作事件
const handleRecordDetail = (record: any) => {
  console.log('查看错题详情:', record)
  // TODO: 实现错题详情查看逻辑
}

const handleRecordDelete = (record: any) => {
  console.log('删除错题记录:', record)
  // TODO: 实现错题删除逻辑
}
</script>

<style scoped>
.error-collection {
  min-height: 100vh;
  background-color: #f8f9fa;
}

/* 面包屑导航 */
.breadcrumb {
  padding: 16px 24px;
  font-size: 14px;
  color: #606266;
}

.breadcrumb-item {
  color: #606266;
}

.breadcrumb-item.current {
  color: #303133;
}

.breadcrumb-separator {
  margin: 0 8px;
  color: #c0c4cc;
}

/* 主内容区域 */
.main-content {
  background: #fff;
  margin: 0 24px;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
}

/* 页面标题 */
.page-header {
  margin-bottom: 24px;
}

.page-title {
  font-size: 20px;
  font-weight: 600;
  color: #303133;
  margin: 0;
}

/* 学科分类标签 */
.subject-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 16px;
  border-bottom: 1px solid #e4e7ed;
}

.subject-tab {
  padding: 12px 24px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  position: relative;
  transition: all 0.3s ease;
}

.subject-tab.active {
  color: #409eff;
}

.subject-tab.active::after {
  content: '';
  position: absolute;
  bottom: -1px;
  left: 0;
  right: 0;
  height: 2px;
  background: #409eff;
}

.subject-tab:hover:not(.active) {
  color: #303133;
}

/* 类型分类标签 */
.type-tabs {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 24px;
}

.type-tab {
  padding: 6px 16px;
  border-radius: 16px;
  font-size: 14px;
  color: #606266;
  cursor: pointer;
  transition: all 0.3s ease;
  background: transparent;
}

.type-tab.active {
  color: #409eff;
  background: #ecf5ff;
}

.type-tab:hover:not(.active) {
  color: #303133;
  background: #f5f7fa;
}

/* 搜索框 */
.search-box {
  margin-left: auto;
  position: relative;
  width: 240px;
}

.search-input {
  width: 100%;
  height: 32px;
  padding: 0 32px 0 12px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.3s ease;
}

.search-input:focus {
  border-color: #409eff;
}

.search-input::placeholder {
  color: #c0c4cc;
}

.search-icon {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: #c0c4cc;
  font-size: 16px;
}

/* 记录卡片网格 */
.records-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 20px;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .error-collection {
    padding: 0;
  }
  
  .breadcrumb {
    padding: 12px 16px;
  }
  
  .main-content {
    margin: 0 16px;
    padding: 16px;
  }
  
  .subject-tabs {
    flex-wrap: wrap;
  }
  
  .type-tabs {
    flex-wrap: wrap;
    gap: 12px;
  }
  
  .search-box {
    width: 100%;
    margin-left: 0;
    margin-top: 12px;
  }
  
  .records-grid {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

@media (max-width: 480px) {
  .page-title {
    font-size: 18px;
  }
  
  .subject-tab {
    padding: 10px 16px;
    font-size: 13px;
  }
  
  .type-tab {
    padding: 4px 12px;
    font-size: 13px;
  }
}
</style>
